package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"logi-flow/cache"
	"logi-flow/models"
	"strconv"
	"time"
)

func PerSonAdd(c *gin.Context) {
	person_code := c.PostForm("person_code")
	person_name := c.PostForm("person_name")
	person_phone := c.PostForm("person_phone")
	data := models.PerSon{
		PerSonCode:  person_code,
		PerSonName:  person_name,
		PerSonPhone: person_phone,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PerSonOrderAdd(c *gin.Context) {
	person_order_code := c.PostForm("person_order_code")
	person_order_address := c.PostForm("person_order_address")
	pid, _ := strconv.Atoi(c.PostForm("pid"))
	data := models.PerSonOrder{
		PerSonOrderCode:    person_order_code,
		PerSonOrderAddress: person_order_address,
		Pid:                pid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func PersonOrderList(c *gin.Context) {
	/*
		SELECT
		  per_son_orders.id AS OrderId,
		  per_son_orders.per_son_order_code AS PerSonOrderCode,
		  per_son_orders.per_son_order_address AS PerSonOrderAddress,
		  per_sons.per_son_code AS PerSonCode,
		  per_sons.per_son_name AS PerSonName,
		  per_sons.per_son_phone AS PerSonPhone
		FROM
		  per_son_orders
		  LEFT JOIN per_sons ON per_son_orders.pid = per_sons.id
		  WHERE per_son_orders.id=1
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var list []models.PerSonOrderList
	redis_key := "person_order_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("per_son_orders").
			Select(
				"per_son_orders.id AS OrderId",
				"per_son_orders.per_son_order_code AS PerSonOrderCode",
				"per_son_orders.per_son_order_address AS PerSonOrderAddress",
				"per_sons.per_son_code AS PerSonCode",
				"per_sons.per_son_name AS PerSonName",
				"per_sons.per_son_phone AS PerSonPhone",
			).
			Joins(" LEFT JOIN per_sons ON per_son_orders.pid = per_sons.id").
			Offset(offset).Limit(size).Find(&list)

		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	var total int64
	models.DB.Model(&models.PerSonOrder{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"data":  list,
		"total": total,
		"msg":   "ok",
	})
	return

}
func PersonOrderDetail(c *gin.Context) {
	/*
		SELECT
		  per_son_orders.id AS OrderId,
		  per_son_orders.per_son_order_code AS PerSonOrderCode,
		  per_son_orders.per_son_order_address AS PerSonOrderAddress,
		  per_sons.per_son_code AS PerSonCode,
		  per_sons.per_son_name AS PerSonName,
		  per_sons.per_son_phone AS PerSonPhone
		FROM
		  per_son_orders
		  LEFT JOIN per_sons ON per_son_orders.pid = per_sons.id
		  WHERE per_son_orders.id=1
	*/
	id := c.Query("id")
	var list models.PerSonOrderList
	redis_key := "person_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("per_son_orders").
			Select(
				"per_son_orders.id AS OrderId",
				"per_son_orders.per_son_order_code AS PerSonOrderCode",
				"per_son_orders.per_son_order_address AS PerSonOrderAddress",
				"per_sons.per_son_code AS PerSonCode",
				"per_sons.per_son_name AS PerSonName",
				"per_sons.per_son_phone AS PerSonPhone",
			).
			Joins(" LEFT JOIN per_sons ON per_son_orders.pid = per_sons.id").
			Where("per_son_orders.id=?", id).Find(&list)

		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "ok",
	})
	return

}
